<template>
  <div class="loginbg">
    <!-- <h1 class="tc color1 pt65"></h1> -->
    <el-row>
      <el-col :span="12">&ensp;</el-col>
      <el-col :span="12">
        <h1 class="tc color1 mt140 f36">博客后台管理</h1>
        <div class="loginBox tc">
          <span class="color1 f18 pt20">登录</span>
          <el-form ref="loginForm" :model="loginForm" :rules="rules" @submit.native.prevent>
            <el-form-item label class="mt20" prop="username">
              <el-input v-model="loginForm.username" placeholder="请输入用户名"></el-input>
            </el-form-item>
            <el-form-item label prop="password">
              <el-input type="password" v-model="loginForm.password" placeholder="请输入密码"></el-input>
            </el-form-item>
            <el-form-item size="medium" class="mt55">
              <el-button
                type="primary"
                class="wb100"
                native-type="submit"
                :loading="loading"
                @click="sub"
              >登录</el-button>
              <!-- <el-button>取消</el-button> -->
            </el-form-item>
          </el-form>
        </div>
      </el-col>
    </el-row>
  </div>
</template>
<script>
import { znk } from '@/assets/util/public'
export default {
  head() {
    return {
      meta: [
        {login:true }
      ]
    }
  },
  // name: 'HelloWorld',
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      },
      loading: false
    }
  },
  methods: {
    sub() {
      this.$refs['loginForm'].validate(valid => {
        if (valid) {
          this.loading = true
          this.$Http('POST', '/api/admin/login', {
            username: this.loginForm.username,
            password: this.loginForm.password
          }).then(data => {
            if (data.code === 0) {
              znk.setCookie('token', data.data.access_token)
              this.$store.commit('TOKEN',data.data.access_token)
              console.log('login',this)
              this.$router.push({
                path: '/'
              })
            } else {
              this.loading = false
            }
          }).catch(data => {
            console.log('44')
            this.loading = false
          })
        }
      })
    }
  },
  mounted() {
    //   this.$Http('get', '/adminApi/user/userList').then(data => {
    //     if (data.code === 0) {
    //       // znk.setCookie('token', data.data)
    //     } else {
    //       this.loading = false
    //     }
    //   }).catch(data => {
    //     console.log('44')
    //     this.loading = false
    //   })
  }
}
</script>
<style lang="css" scoped>
.loginbg {
  position: relative;
  height: 100%;
  min-height: 500px;
  background: url("../assets/images/login_bg1.jpg");
  background-size: cover;
  background-position: center;
}
.loginBox {
  margin-top: 6%;
  margin-left: auto;
  margin-right: auto;
  padding: 20px 20px;
  width: 320px;
  min-height: 260px;
  background-color: #bacafb;
  border-radius: 2px;
}
</style>
